package com.jushu.storbox.util;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.jushu.storbox.R;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.view.dialog.ClouddriveDialog;

public class CloudDriveDialogUtil{
	
	
	public static ClouddriveDialog getCloudDriveDialog(Context context, int textResId, OnClickListener positiveOnClickListener , OnClickListener negetiveOnClickListener){
		return getCloudDriveDialog(context, context.getString(textResId), positiveOnClickListener, negetiveOnClickListener);
	}
	
	public static ClouddriveDialog getCloudDriveDialog(Context context, String text, OnClickListener positiveOnClickListener , OnClickListener negetiveOnClickListener){
		final ClouddriveDialog dialog = new ClouddriveDialog(context,
				R.style.dialog_upload, R.layout.dialog_filedelete);
		dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		View conventView = dialog.getConventView();
		Button dialog_cancal = (Button) conventView
				.findViewById(R.id.dialog_add_file_cancal_bt);

		Button dialog_OK = (Button) conventView
				.findViewById(R.id.dialog_add_file_ok_bt);

		TextView dialog_prompt_tv = (TextView) conventView
				.findViewById(R.id.dialog_prompt_tv);
		dialog_prompt_tv.setText(text);
		if(negetiveOnClickListener == null){
			dialog_cancal.setVisibility(View.GONE);
		}else{
			MyInvocationHandler myInvocationHandler = new MyInvocationHandler(negetiveOnClickListener, dialog);
			dialog_cancal.setOnClickListener((OnClickListener)Proxy.newProxyInstance(negetiveOnClickListener.getClass().getClassLoader(), negetiveOnClickListener.getClass().getInterfaces(), myInvocationHandler));
		}
		
		if(positiveOnClickListener == null){
			dialog_OK.setVisibility(View.GONE);
		}else{
			MyInvocationHandler myInvocationHandler = new MyInvocationHandler(positiveOnClickListener, dialog);
			dialog_OK.setOnClickListener((OnClickListener)Proxy.newProxyInstance(positiveOnClickListener.getClass().getClassLoader(), positiveOnClickListener.getClass().getInterfaces(), myInvocationHandler));
		}
		return dialog;
	}
	
	private static class MyInvocationHandler implements InvocationHandler{
		private Object target;
		private Dialog dialog;
		
		
		public MyInvocationHandler(Object target, Dialog dialog) {
			super();
			this.target = target;
			this.dialog = dialog;
		}
		
		public void setTarget(Object target){
			this.target = target;
		}
		
		
		public void setDialog(Dialog dialog){
			this.dialog = dialog;
		}

		@Override
		public Object invoke(Object proxy, Method method, Object[] args)
				throws Throwable {
			dialog.dismiss();
			method.invoke(target, args);
			return null;
		}
		
	}
	
//	public static ClouddriveDialog getFixAnyofficeDialog(Context context){
//		final ClouddriveDialog dialog = new ClouddriveDialog(context,
//				R.style.dialog_upload, R.layout.dialog_anyoffice);
//		dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
//		View conventView = dialog.getConventView();
//		Button dialog_cancal = (Button) conventView
//				.findViewById(R.id.dialog_add_file_cancal_bt);
//
//		Button dialog_OK = (Button) conventView
//				.findViewById(R.id.dialog_add_file_ok_bt);
//
//		final CheckBox checkBox_notify = (CheckBox) conventView.findViewById(R.id.checkbox_is_notify);
//		
//		final Context mContext = context.getApplicationContext();
//		
//		dialog_cancal.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				dialog.dismiss();
//				if(checkBox_notify.isChecked()){
//					SharedPreferences preferences = mContext.getSharedPreferences(ClientConfig.settings,
//							Context.MODE_APPEND);
//					Editor editor = preferences.edit();
//					editor.putBoolean(ClientConfig.NOT_NEED_NOTIFY, true);
//					editor.commit();
//				}
//			}
//		});
//		
//		dialog_OK.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				dialog.dismiss();
//			}
//		});
////		dialog.setCanceledOnTouchOutside(true);
//		return dialog;
//	}
}
